package v578c82f6;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.util.ArrayMap;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/* loaded from: classes.dex */
public class l578c82f6 extends Application {
    public static String g;
    public static String h;
    public static Context i;
    public String b;
    public String c;
    public Application d;
    public Object e;
    public String f;

    /* loaded from: classes.dex */
    public class jntm implements Application.ActivityLifecycleCallbacks {
        public jntm(l578c82f6 l578c82f6Var) {
        }

        @Override // android.app.Application.ActivityLifecycleCallbacks
        public void onActivityCreated(Activity activity, Bundle bundle) {
            l578c82f6.I578c82f6_02(activity, bundle);
        }

        @Override // android.app.Application.ActivityLifecycleCallbacks
        public void onActivityDestroyed(Activity activity) {
            l578c82f6.I578c82f6_04(activity);
        }

        @Override // android.app.Application.ActivityLifecycleCallbacks
        public void onActivityPaused(Activity activity) {
        }

        @Override // android.app.Application.ActivityLifecycleCallbacks
        public void onActivityResumed(Activity activity) {
        }

        @Override // android.app.Application.ActivityLifecycleCallbacks
        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
        }

        @Override // android.app.Application.ActivityLifecycleCallbacks
        public void onActivityStarted(Activity activity) {
            l578c82f6.I578c82f6_03(activity);
        }

        @Override // android.app.Application.ActivityLifecycleCallbacks
        public void onActivityStopped(Activity activity) {
        }
    }

    public l578c82f6() {
        g = "Virbox";
        h = l578c82f6.class.getSimpleName();
    }

    public static native void I578c82f6_00(Context context);

    public static native void (Context context, Application application, String str);

    public static native void I578c82f6_02(Activity activity, Bundle bundle);

    public static native void I578c82f6_03(Activity activity);

    public static native void I578c82f6_04(Activity activity);

    /* JADX WARN: Type inference failed for: r0v0, types: [java.lang.reflect.AccessibleObject, java.net.SetAccessible, java.lang.StringBuilder] */
    /* JADX WARN: Type inference failed for: r7v2, types: [java.net.SetAccessible, java.lang.StringBuilder] */
    public static boolean c(Context context, String str, String str2, String str3) {
        ?? r0;
        ?? r7;
        r0.add(r0);
        r0.append(String.valueOf(str2));
        r0.append(File.separator);
        r0.append(str3);
        String sb = r0.toString();
        File file = new File(str2);
        if (!file.exists()) {
            file.mkdir();
        }
        File file2 = new File(sb);
        try {
            if (file2.exists()) {
                InputStream open = context.getResources().getAssets().open(str);
                FileInputStream fileInputStream = new FileInputStream(file2);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(open);
                BufferedInputStream bufferedInputStream2 = new BufferedInputStream(fileInputStream);
                boolean z = g(bufferedInputStream, bufferedInputStream2);
                open.close();
                fileInputStream.close();
                bufferedInputStream.close();
                bufferedInputStream2.close();
                if (z) {
                    return true;
                }
            }
            InputStream open2 = context.getResources().getAssets().open(str);
            FileOutputStream fileOutputStream = new FileOutputStream(sb);
            byte[] bArr = new byte[7168];
            while (true) {
                int read = open2.read(bArr);
                if (read <= 0) {
                    break;
                }
                fileOutputStream.write(bArr, 0, read);
            }
            fileOutputStream.close();
            open2.close();
            try {
                Runtime runtime = Runtime.getRuntime();
                r7.add(null);
                r7.append("chmod 755 ");
                r7.append(sb);
                runtime.exec(r7.toString());
            } catch (Exception unused) {
            }
            return true;
        } catch (FileNotFoundException | IOException unused2) {
            return false;
        }
    }

    /* JADX WARN: Type inference failed for: r0v0, types: [java.lang.reflect.AccessibleObject, java.net.SetAccessible, java.lang.StringBuilder] */
    public static Field f(Object obj, String str) {
        ?? r0;
        for (Class<?> cls = obj.getClass(); cls != null; cls = cls.getSuperclass()) {
            try {
                Field declaredField = cls.getDeclaredField(str);
                declaredField.setAccessible(true);
                return declaredField;
            } catch (NoSuchFieldException unused) {
            }
        }
        r0.add(r0);
        r0.append("Field '");
        r0.append(str);
        r0.append("' not found");
        throw new IllegalStateException(r0.toString());
    }

    public static boolean g(BufferedInputStream bufferedInputStream, BufferedInputStream bufferedInputStream2) {
        try {
            int available = bufferedInputStream.available();
            int available2 = bufferedInputStream2.available();
            if (available != available2) {
                return false;
            }
            byte[] bArr = new byte[available];
            byte[] bArr2 = new byte[available2];
            bufferedInputStream.read(bArr);
            bufferedInputStream2.read(bArr2);
            for (int i2 = 0; i2 < available; i2++) {
                if (bArr[i2] != bArr2[i2]) {
                    return false;
                }
            }
            return true;
        } catch (Exception unused) {
            return false;
        }
    }

    public static Context getAppContext() {
        return i;
    }

    public static String h(byte[] bArr) {
        for (int i2 = 0; i2 < bArr.length; i2++) {
            bArr[i2] = (byte) (bArr[i2] ^ 105);
        }
        return new String(bArr, 0, bArr.length);
    }

    public final void a() {
        Class<?> cls;
        Field field;
        try {
            Class<?> cls2 = Class.forName("android.app.ActivityThread");
            Method method = cls2.getMethod("currentActivityThread", new Class[0]);
            method.setAccessible(true);
            Object invoke = method.invoke(null, new Object[0]);
            Field declaredField = cls2.getDeclaredField("mInitialApplication");
            declaredField.setAccessible(true);
            if (((Application) declaredField.get(invoke)) == this) {
                declaredField.set(invoke, this.d);
            }
            Field declaredField2 = cls2.getDeclaredField("mAllApplications");
            declaredField2.setAccessible(true);
            List list = (List) declaredField2.get(invoke);
            for (int i2 = 0; i2 < list.size(); i2++) {
                if (list.get(i2) == this) {
                    list.set(i2, this.d);
                }
            }
            try {
                cls = Class.forName("android.app.LoadedApk");
            } catch (ClassNotFoundException unused) {
                cls = Class.forName("android.app.ActivityThread$PackageInfo");
            }
            Field declaredField3 = cls.getDeclaredField("mApplication");
            declaredField3.setAccessible(true);
            Field declaredField4 = cls.getDeclaredField("mResDir");
            declaredField4.setAccessible(true);
            try {
                field = Application.class.getDeclaredField("mLoadedApk");
            } catch (NoSuchFieldException unused2) {
                field = null;
            }
            String[] strArr = {"mPackages", "mResourcePackages"};
            for (int i3 = 0; i3 < 2; i3++) {
                Field declaredField5 = cls2.getDeclaredField(strArr[i3]);
                declaredField5.setAccessible(true);
                for (Map.Entry entry : ((Map) declaredField5.get(invoke)).entrySet()) {
                    Object obj = ((WeakReference) entry.getValue()).get();
                    if (obj != null && declaredField3.get(obj) == this) {
                        declaredField3.set(obj, this.d);
                        if (this.f != null) {
                            declaredField4.set(obj, this.f);
                        }
                        if (field != null) {
                            field.set(this.d, obj);
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

  
    @Override // android.content.ContextWrapper
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        i = context;
        String absolutePath = context.getFilesDir().getAbsolutePath();
        File file = new File(absolutePath);
        if (!file.exists()) {
            file.mkdir();
        }
        r4.add(this);
        r4.append(absolutePath);
        r4.append("/.ss");
        String sb = r4.toString();
        File file2 = new File(sb);
        if (!file2.exists()) {
            file2.mkdir();
        }
        this.c = getPackageName();
        try {
            getPackageName();
            new Object();
            Bundle bundle = ((ApplicationInfo) r7).metaData;
            if (bundle != null && bundle.containsKey("SAPP_NAME")) {
                String string = bundle.getString("SAPP_NAME");
                this.b = string;
                if (string != null && string.length() > 0) {
                    int i2 = 0;
                    int i3 = 0;
                    while (true) {
                        int indexOf = this.b.indexOf(".", i2);
                        if (indexOf == -1) {
                            break;
                        }
                        i2 = indexOf + 1;
                        i3++;
                    }
                    if (i3 == 0) {
                        r72.add(this);
                        r72.append(this.c);
                        r72.append(".");
                        r72.append(this.b);
                        this.b = r72.toString();
                    } else if (i3 == 1 && this.b.startsWith(".")) {
                        r225.add(this);
                        r225.append(this.c);
                        r225.append(this.b);
                        this.b = r225.toString();
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException unused) {
        }
        String str = Build.CPU_ABI;
        try {
            if (Build.VERSION.SDK_INT >= 21) {
                z = false;
                for (String str2 : Build.SUPPORTED_ABIS) {
                    try {
                        if (str2.matches("x86") || str2.matches("x86_64")) {
                            z = true;
                        }
                    } catch (Exception unused2) {
                    }
                }
                if (z) {
                    if (str.contains("armeabi")) {
                        r211.add(this);
                        r211.append(h);
                        r211.append("_a32.so");
                        String sb2 = r211.toString();
                        r56.add(this);
                        r56.append(h);
                        r56.append(".so");
                        c(context, sb2, sb, r56.toString());
                        r212.add(this);
                        r212.append(sb);
                        r212.append(File.separator);
                        r212.append(h);
                        r212.append(".so");
                        System.load(r212.toString());
                        I578c82f6_00(context);
                    } else if (str.contains("arm64")) {
                        r29.add(this);
                        r29.append(h);
                        r29.append("_a64.so");
                        String sb3 = r29.toString();
                        r55.add(this);
                        r55.append(h);
                        r55.append(".so");
                        c(context, sb3, sb, r55.toString());
                        r210.add(this);
                        r210.append(sb);
                        r210.append(File.separator);
                        r210.append(h);
                        r210.append(".so");
                        System.load(r210.toString());
                        I578c82f6_00(context);
                    } else if (str.contains("x86_64")) {
                        r27.add(this);
                        r27.append(h);
                        r27.append("_x64.so");
                        String sb4 = r27.toString();
                        r54.add(this);
                        r54.append(h);
                        r54.append(".so");
                        c(context, sb4, sb, r54.toString());
                        r28.add(this);
                        r28.append(sb);
                        r28.append(File.separator);
                        r28.append(h);
                        r28.append(".so");
                        System.load(r28.toString());
                        I578c82f6_00(context);
                    } else if (str.contains("x86")) {
                        r25.add(this);
                        r25.append(h);
                        r25.append("_x86.so");
                        String sb5 = r25.toString();
                        r53.add(this);
                        r53.append(h);
                        r53.append(".so");
                        c(context, sb5, sb, r53.toString());
                        r26.add(this);
                        r26.append(sb);
                        r26.append(File.separator);
                        r26.append(h);
                        r26.append(".so");
                        System.load(r26.toString());
                        I578c82f6_00(context);
                    } else if (str.contains("mips64")) {
                        r23.add(this);
                        r23.append(h);
                        r23.append("_m64.so");
                        String sb6 = r23.toString();
                        r52.add(this);
                        r52.append(h);
                        r52.append(".so");
                        c(context, sb6, sb, r52.toString());
                        r24.add(this);
                        r24.append(sb);
                        r24.append(File.separator);
                        r24.append(h);
                        r24.append(".so");
                        System.load(r24.toString());
                        I578c82f6_00(context);
                    } else if (!str.contains("mips")) {
                        String str3 = g;
                        r3.add(this);
                        r3.append("Not supported abi:");
                        r3.append(str);
                        Log.e(str3, r3.toString());
                        return;
                    } else {
                        r2.add(this);
                        r2.append(h);
                        r2.append("_mips.so");
                        String sb7 = r2.toString();
                        r5.add(this);
                        r5.append(h);
                        r5.append(".so");
                        c(context, sb7, sb, r5.toString());
                        r22.add(this);
                        r22.append(sb);
                        r22.append(File.separator);
                        r22.append(h);
                        r22.append(".so");
                        System.load(r22.toString());
                        I578c82f6_00(context);
                    }
                } else if (str.contains("armeabi")) {
                    r223.add(this);
                    r223.append(h);
                    r223.append("_x86.so");
                    String sb8 = r223.toString();
                    r512.add(this);
                    r512.append(h);
                    r512.append(".so");
                    c(context, sb8, sb, r512.toString());
                    r224.add(this);
                    r224.append(sb);
                    r224.append(File.separator);
                    r224.append(h);
                    r224.append(".so");
                    System.load(r224.toString());
                    I578c82f6_00(context);
                } else if (str.contains("arm64")) {
                    r221.add(this);
                    r221.append(h);
                    r221.append("_x64.so");
                    String sb9 = r221.toString();
                    r511.add(this);
                    r511.append(h);
                    r511.append(".so");
                    c(context, sb9, sb, r511.toString());
                    r222.add(this);
                    r222.append(sb);
                    r222.append(File.separator);
                    r222.append(h);
                    r222.append(".so");
                    System.load(r222.toString());
                    I578c82f6_00(context);
                } else if (str.contains("x86_64")) {
                    r219.add(this);
                    r219.append(h);
                    r219.append("_x64.so");
                    String sb10 = r219.toString();
                    r510.add(this);
                    r510.append(h);
                    r510.append(".so");
                    c(context, sb10, sb, r510.toString());
                    r220.add(this);
                    r220.append(sb);
                    r220.append(File.separator);
                    r220.append(h);
                    r220.append(".so");
                    System.load(r220.toString());
                    I578c82f6_00(context);
                } else if (str.contains("x86")) {
                    r217.add(this);
                    r217.append(h);
                    r217.append("_x86.so");
                    String sb11 = r217.toString();
                    r59.add(this);
                    r59.append(h);
                    r59.append(".so");
                    c(context, sb11, sb, r59.toString());
                    r218.add(this);
                    r218.append(sb);
                    r218.append(File.separator);
                    r218.append(h);
                    r218.append(".so");
                    System.load(r218.toString());
                    I578c82f6_00(context);
                } else if (str.contains("mips64")) {
                    r215.add(this);
                    r215.append(h);
                    r215.append("_m64.so");
                    String sb12 = r215.toString();
                    r58.add(this);
                    r58.append(h);
                    r58.append(".so");
                    c(context, sb12, sb, r58.toString());
                    r216.add(this);
                    r216.append(sb);
                    r216.append(File.separator);
                    r216.append(h);
                    r216.append(".so");
                    System.load(r216.toString());
                    I578c82f6_00(context);
                } else if (!str.contains("mips")) {
                    String str4 = g;
                    r32.add(this);
                    r32.append("Not supported abi:");
                    r32.append(str);
                    Log.e(str4, r32.toString());
                    return;
                } else {
                    r213.add(this);
                    r213.append(h);
                    r213.append("_mips.so");
                    String sb13 = r213.toString();
                    r57.add(this);
                    r57.append(h);
                    r57.append(".so");
                    c(context, sb13, sb, r57.toString());
                    r214.add(this);
                    r214.append(sb);
                    r214.append(File.separator);
                    r214.append(h);
                    r214.append(".so");
                    System.load(r214.toString());
                    I578c82f6_00(context);
                }
                getBaseContext();
                if (this.b != null && this.b.length() > 0) {
                    Context baseContext = getBaseContext();
                    this.d = (Application) getClassLoader().loadClass(this.b).newInstance();
                    Method declaredMethod = Application.class.getDeclaredMethod("attach", Context.class);
                    declaredMethod.setAccessible(true);
                    declaredMethod.invoke(this.d, baseContext);
                    d();
                }
                if (this.d == null) {
                    this.d.registerActivityLifecycleCallbacks(new jntm(this));
                    return;
                } else {
                    registerActivityLifecycleCallbacks(new jntm(this));
                    return;
                }
            }
            if (this.d == null) {
            }
        } catch (Exception unused3) {
            return;
        }
        z = false;
        if (z) {
        }
        getBaseContext();
        if (this.b != null) {
            Context baseContext2 = getBaseContext();
            this.d = (Application) getClassLoader().loadClass(this.b).newInstance();
            Method declaredMethod2 = Application.class.getDeclaredMethod("attach", Context.class);
            declaredMethod2.setAccessible(true);
            declaredMethod2.invoke(this.d, baseContext2);
            d();
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v5, types: [android.content.res.AssetManager, boolean, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r7v44, types: [java.lang.reflect.AccessibleObject, android.util.ArrayMap] */
    public final void b() {
        Collection<WeakReference> collection;
        if (this.f == null) {
            return;
        }
        try {
            ?? r0 = (AssetManager) AssetManager.class.getConstructor(new Class[0]).newInstance(new Object[0]);
            Method declaredMethod = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
            declaredMethod.setAccessible(true);
            if (((Integer) declaredMethod.invoke(r0, this.f)).intValue() == 0) {
                throw new IllegalStateException("Could not create new AssetManager");
            }
            if (Build.VERSION.SDK_INT <= 19) {
                Method declaredMethod2 = AssetManager.class.getDeclaredMethod("ensureStringBlocks", new Class[0]);
                declaredMethod2.setAccessible(true);
                declaredMethod2.invoke(r0, new Object[0]);
            }
            Class<?> cls = Class.forName("android.app.ResourcesManager");
            Method declaredMethod3 = cls.getDeclaredMethod("getInstance", new Class[0]);
            declaredMethod3.setAccessible(true);
            Object invoke = declaredMethod3.invoke(null, new Object[0]);
            try {
                Field declaredField = cls.getDeclaredField("mActiveResources");
                declaredField.setAccessible(true);
                collection = ((ArrayMap) declaredField.get(invoke)).setAccessible(r0);
            } catch (NoSuchFieldException unused) {
                Field declaredField2 = cls.getDeclaredField("mResourceReferences");
                declaredField2.setAccessible(true);
                collection = (Collection) declaredField2.get(invoke);
            }
            for (WeakReference weakReference : collection) {
                Resources resources = (Resources) weakReference.get();
                try {
                    Field declaredField3 = Resources.class.getDeclaredField("mAssets");
                    declaredField3.setAccessible(true);
                    declaredField3.set(resources, r0);
                } catch (NoSuchFieldException unused2) {
                    Field declaredField4 = Resources.class.getDeclaredField("mResourcesImpl");
                    declaredField4.setAccessible(true);
                    Object obj = declaredField4.get(resources);
                    Field declaredField5 = obj.getClass().getDeclaredField("mAssets");
                    declaredField5.setAccessible(true);
                    declaredField5.set(obj, r0);
                }
                resources.updateConfiguration(resources.getConfiguration(), resources.getDisplayMetrics());
            }
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    public final void d() {
        try {
            Method method = Class.forName("android.app.ActivityThread").getMethod("currentActivityThread", new Class[0]);
            method.setAccessible(true);
            Object invoke = method.invoke(null, new Object[0]);
            Object obj = f(invoke, "mBoundApplication").get(invoke);
            Field f = f(obj, "providers");
            try {
                this.e = f.get(obj);
                f.set(obj, null);
            } catch (Exception e) {
                e = e;
                throw new IllegalStateException(e);
            }
        } catch (Exception e2) {
            e = e2;
        }
    }

    public final void e() {
        try {
            Class<?> cls = Class.forName("android.app.ActivityThread");
            Method method = cls.getMethod("currentActivityThread", new Class[0]);
            method.setAccessible(true);
            Object invoke = method.invoke(null, new Object[0]);
            Object obj = f(invoke, "mBoundApplication").get(invoke);
            f(obj, "providers").set(obj, this.e);
            if (this.e != null) {
                Method declaredMethod = cls.getDeclaredMethod("installContentProviders", Context.class, List.class);
                declaredMethod.setAccessible(true);
                declaredMethod.invoke(invoke, this.d, this.e);
                this.e = null;
            }
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    @Override // android.app.Application
    public void onCreate() {
        if (this.d != null) {
            try {
                a();
                b();
                e();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.onCreate();
        Application application = this.d;
        if (application != null) {
            try {
                application.onCreate();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
}